﻿#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 ZHENG Zhong <http://www.zhengzhong.net/>
#
# Created on 2008-05-06.
# $Id$
#

import logging
import os

from django.template import loader, Context
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, HttpResponseNotFound

from google.appengine.api import users

from buggarden.common import google_keys

#---------------------------------------------------------------------------------------------------
# Utility functions
#---------------------------------------------------------------------------------------------------

def _create_context(request, ctxt_dict):
    """Returns a Django context."""
    user = users.get_current_user()
    if not user:
        sign_in_url = users.create_login_url(request.path)
        sign_out_url = None
    else:
        sign_in_url = None
        sign_out_url = users.create_logout_url(request.path)
    ctxt_dict.update({
        'user_': user,
        'sign_in_url_': sign_in_url,
        'logout_url_': sign_out_url,
        'webapp_': '',
        'static_': '/static/buggarden',
        'ajax_api_key_': google_keys.AJAX_API_KEY,
        'analytics_key_': google_keys.ANALYTICS_KEY,
    })
    return Context(ctxt_dict)


def _get_template(name):
    """Returns a Django template."""
    return loader.get_template('buggarden/%s' % name)


#---------------------------------------------------------------------------------------------------

def home(request):
    """Returns the bug garden website homepage."""
    ctxt = _create_context(request, {})
    tmpl = _get_template('home.html')
    return HttpResponse(tmpl.render(ctxt))

#---------------------------------------------------------------------------------------------------

def not_found(request):
    """Returns the 404 not-found page."""
    logging.info('The requested page "%s" does not exist.' % request.path)
    ctxt = _create_context(request, {'url': request.path,})
    tmpl = _get_template('not_found.html')
    return HttpResponseNotFound(tmpl.render(ctxt))

#---------------------------------------------------------------------------------------------------

def logos(request):
    """Returns the bug garden's logos."""
    ctxt = _create_context(request, {})
    tmpl = _get_template('logos.html')
    return HttpResponse(tmpl.render(ctxt))

#---------------------------------------------------------------------------------------------------

def webapp_info(request):
    """Returns information about the webapp environment and the incoming request."""
    ctxt_dict = { 'os_environ': dict(os.environ), 'request_meta': dict(request.META), }
    ctxt = _create_context(request, ctxt_dict)
    tmpl = _get_template('webapp_info.html')
    return HttpResponse(tmpl.render(ctxt))


#---------------------------------------------------------------------------------------------------
# Sandbox for testing new features
#---------------------------------------------------------------------------------------------------

def sandbox(request):
    pass



